package cfm.com.cfmscreen.comm;


import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.media.Ringtone;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;

import cfm.com.cfmscreen.MainActivity;
import cfm.com.cfmscreen.R;

public class Utils {

    // 两次点击按钮之间的点击间隔不能少于1000毫秒
    private static final int MIN_CLICK_DELAY_TIME = 800;
    private static long lastClickTime;
    private static int x = 0;

    public static boolean isFastClick() {
        boolean flag = false;
        long curClickTime = System.currentTimeMillis();
        if ((curClickTime - lastClickTime) >= MIN_CLICK_DELAY_TIME) {
            flag = true;
            lastClickTime = curClickTime;
        }
        return flag;
    }

    public static void showDialog(Context context, String message, final Ringtone r){

        AlertDialog alertDialog = new AlertDialog.Builder(
                context,AlertDialog.THEME_HOLO_LIGHT).create();
        alertDialog.setTitle("预警提示");
        alertDialog.setIcon(R.drawable.ts);
        alertDialog.setMessage(message);
        alertDialog.setButton("退出",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        r.stop();
                    }
                });
        Window win = alertDialog.getWindow();
        WindowManager.LayoutParams params = new WindowManager.LayoutParams();
        x+=20;
        if(x==340){
            x=0;
        }
        params.x = -160+x;//设置x坐标
        params.y = -80+x;//设置y坐标
        params.width = 60;
        params.height = 40;
        win.setAttributes(params);
        alertDialog.setCanceledOnTouchOutside(true);//设置点击Dialog外部任意区域关闭Dialog
        alertDialog.show();
    }

    public static boolean isNetConnected(Context context) {
        boolean isNetConnected;
        // 获得网络连接服务
        ConnectivityManager connManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = connManager.getActiveNetworkInfo();
        if (info != null && info.isAvailable()) {
            isNetConnected = true;
        } else {
            isNetConnected = false;
        }
        return isNetConnected;
    }
}
